﻿using Fawcett.Plugins.Controls;
using NCalc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Fawcett.Plugins.Panels
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();

            GlobalEvent.MyEvent += this.PluginEventHandler;

            MappingCollection bar = (MappingCollection)ConfigurationManager.GetSection("WarningPanel");

            foreach (Mapping mapping in bar)
            {
                new_button(mapping.Name , mapping.On , mapping.Off , mapping.BackOn , mapping.BackOff);

            }
        }


        private void new_button( string Name , Color on , Color off  , Color back_on , Color back_off )
        {
            DualPushButton dualPushButton1 = new DualPushButton();
    
            dualPushButton1.Name = Name;
            dualPushButton1.Size = new System.Drawing.Size(75, 76);
            dualPushButton1.TabIndex = 0;
            dualPushButton1.text = Name;
            dualPushButton1.forebrush = new SolidBrush( off );
            dualPushButton1.onbrush = new SolidBrush( on );
            dualPushButton1.backon = new SolidBrush(back_on);
            dualPushButton1.backoff = new SolidBrush(back_off);
            dualPushButton1.button_state = DualPushButton.State.OFF;

            Panel1.Controls.Add(dualPushButton1);
        }
        
        private Controls.DualPushButton GetButton( string name )
        {
            Controls.DualPushButton btn = null;

            for (int i = 0; i < this.Panel1.Controls.Count; i++)
            {
                btn = (Controls.DualPushButton)this.Panel1.Controls[i];
                if (btn.Name == name) return btn;
            }

            return null;
        }

        internal Boolean Calculation( String in_str, float p_value)
        {
            Expression e = new Expression( "IN " + in_str);

             e.Parameters["IN"] = Convert.ToSingle(p_value);

            Boolean rval = (Boolean)e.Evaluate();

            Console.WriteLine(  "Function {0}  ({1}) resolves to {2}" , e.ParsedExpression, p_value , rval);
            return rval;
        }
        public void PluginEventHandler(object sender, EPlugin p)
        {

            if (p.GetCode == PluginCode.FROM_FSX)
            {
                if (this.Visible == false) return;

                MappingCollection bar = (MappingCollection)ConfigurationManager.GetSection("WarningPanel");

                foreach (Mapping mapping in bar)
                {
                    if( mapping.Offset == p.GetOffset )
                    {
                        try
                        {
                            if ( Calculation( mapping.Threshold , p.GetValue ) == true )
                                GetButton(mapping.Name).Toggle( DualPushButton.State.ON);
                            else
                                GetButton(mapping.Name).Toggle( DualPushButton.State.OFF);
                        }
                        catch( Exception e)
                        {
                            string str = String.Format(Fawcett.Plugins.Properties.Resources.ERROR_2, mapping.Offset, "WarningPanel");
                            Fawcett.Plugins.MessageBox.Show( e, str);
                       
                        }
                    }

                    }
                }
            }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            e.Cancel = true;

        }
    
        }
    }

    